$(function () {
    $("#jqGrid").Grid({
        url: '../maintainplan/list',
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '计划名称', name: 'planName', index: 'PLAN_NAME', width: 80, align: 'center'},
            // {label: '所属车间', name: 'dept', index: 'DEPT', width: 80, align: 'center'},
            // {label: '生产线', name: 'beltline', index: 'BELTLINE', width: 80, align: 'center'},
            {
                label: '计划开始时间', name: 'carryDate', index: 'CARRY_DATE', width: 80, align: 'center',
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {
                label: '计划完成时间', name: 'completeDate', index: 'COMPLETE_DATE', width: 80, align: 'center',
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {
                label: '计划类别', name: 'types', inde: 'TYPES', width: 120, align: 'center'
                , formatter: function (value) {
                    return sysEnums.PLAN_TYPE[value];
                }
            },


            {label: '评审人', name: 'review', inde: 'REVIEW', width: 120, align: 'center'},
            {
                label: '评审状态', name: 'approvalStatus', inde: 'APPROVAL_STATUS', width: 120, align: 'center',
                formatter: function (v) {
                    if (v == null || v == "null") {
                        return "暂无"
                    } else {
                        return sysEnums.SPARE_STATUS[v];
                    }
                }
            },

            {label: '备注', name: 'remark', index: 'REMARK', width: 80, align: 'center'},
            {
                label: '维护工单', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.order(\"" + row.id + "\")'>&nbsp;工单详情</button>";
                }
            },
            {
                label: '维护项目', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.viewOption(\"" + row.id + "\")'>&nbsp;维护详情</button>";
                }
            },
        ]

    })
})
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        },
    },
    callback: {
        beforeCheck: true,
    }

};
var vm = new Vue({
    el: '#rrapp',
    data: {
        addTab: true,
        showList: true,
        temporaryShow: false,
        assignmentShow: false,
        maintainplan: {
            ruleName: '',
            ruleId: '',
            carryDate: new Date(),
            completeDate: new Date().getTime() + 31536000 * 1000
        },
        maintainplan2: {},
        options: {
            disabledDate:function(date) {
                return date && date.valueOf() < Date.now() - 86400000;
            }
        },
        q: {
            name: '',
        },
        title: '',
        devMaintenPlan: {},
        freRateArray: enumsToList("FRERATE"),
        typeList: enumsToList('MAINTENPLAN_TYPE'),//类型
        devEquFile: [],
        ruleValidate: '',
        ruleValidate1: '',
        reviewValue: {
            name: '',
            review: '',
        },
        ruleInline: {
            // name: [
            //     {required: true, message: '计划名称不能为空', trigger: 'blur'}
            // ],
            // review: [
            //     {required: true, message: '评审内容不能为空', trigger: 'blur'}
            // ]
        },
        assignmentData: {
            name: '',
            review: ''
        },
        assignmentInline: {
            // name: [
            //     {required: true, message: '计划名称不能为空', trigger: 'blur'}
            // ],
            // review: [
            //     {required: true, message: '评审内容不能为空', trigger: 'blur'}
            // ],
            // carryName: [
            //     {required: true, message: '分派人员不能为空', trigger: 'blur'}
            // ]
        },
        cityList: [],
        checkAllGroup: [],
        planPerson: [],
        columns1: [
            {
                title: '设备编号',
                key: 'DEV_CODE',
                align: 'center'
            }, {
                title: '版本',
                key: 'PLAN_VERSION',
                align: 'center'
            }, {
                title: '实施频率',
                key: 'FRE_RATE',
                align: 'center'
            }, {
                title: '检查时间非生产',
                key: 'CHECK_DATE_UPRUD',
                align: 'center'
            }, {
                title: '检查时间生产',
                key: 'CHECK_DATE_PRUD',
                align: 'center'
            }, {
                title: '人次',
                key: 'PER_EV',
                align: 'center'
            }, {
                title: '内容',
                key: 'PLAN_CON',
                align: 'center'
            }, {
                title: '编制人',
                key: 'ORG_USER_NAME',
                align: 'center'
            }, {
                title: '车间编制人',
                key: 'CHECK_USER_NAME',
                align: 'center'
            }, {
                title: '审核状态',
                key: 'CHECK_STATUS',
                align: 'center'
            }, {
                title: '审核时间',
                key: 'CHECK_DATE',
                align: 'center'
            }, {
                title: 'PE授权人',
                key: 'OK_USER_NAME',
                align: 'center'
            }, {
                title: '批准状态',
                key: 'OK_STATUS',
                align: 'center'
            }, {
                title: '批准时间',
                key: 'OK_DATE',
                align: 'center'
            },
        ],
        columns2: [
            {
                title: '计划名称',
                key: 'planName',
                align: 'center'
            }, {
                title: '原因',
                key: 'reason',
                align: 'center'
            }, {
                title: '事项',
                key: 'matter',
                align: 'center'
            }, {
                title: '变更人',
                key: 'userName',
                align: 'center'
            }, {
                title: '变更时间',
                key: 'changeDate',
                align: 'center'
            }, {
                title: '类型',
                key: 'type',
                align: 'center'
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (function (h, params) {
                    return h('div', [h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            click: function click() {
                                vm.okClick(params);
                            }
                        }
                    }, '通过'), h('Button', {
                        props: {
                            type: 'error',
                            size: 'small'
                        },
                        on: {
                            click: function click() {
                                vm.turnDownClick(params);
                            }
                        }
                    }, '驳回')]);
                })
                // render: (h, params) => {
                //     return h('div', [
                //         h('Button', {
                //             props: {type: 'primary', size: 'small'},
                //             style: {marginRight: '5px'},
                //             on: {
                //                 click: () => {
                //                     vm.okClick(params)
                //                 }
                //             }
                //         }, '通过'),
                //         h('Button', {
                //             props: {type: 'error', size: 'small'},
                //             on: {
                //                 click: () => {
                //                     vm.turnDownClick(params)
                //                 }
                //             }
                //         }, '驳回')
                //     ]);
                // }
            }
        ],
        data1: [],
        data2: [],
        planApplyValidate: {
            // planName: [
            //     {required: true, message: '计划名称不能为空', trigger: 'blur'}
            // ],
            // reason: [
            //     {required: true, message: '原因不能为空', trigger: 'blur'}
            // ],
            // matter: [
            //     {required: true, message: '事项不能为空', trigger: 'blur'}
            // ],
            // planApplyType: [
            //     {required: true, message: '类型不能为空', trigger: 'blur'}
            // ]
        },
        planApplyData: {
            planName: '',
            reason: '',
            matter: '',
            maintainPlanId: '',
            type: '',
            planApplyType: '',
        },
        abnormaInline: {
            // abnormalSources: [
            //     {required: true, message: '异常来源不能为空', trigger: 'blur'}
            // ],
            // equipmentId: [
            //     {required: true, message: '设备名称不能为空', trigger: 'blur'}
            // ],
            // productionLine: [
            //     {required: true, message: '生产线不能为空', trigger: 'blur'}
            // ],
            // stationNumber: [
            //     {required: true, message: '工位序号不能为空', trigger: 'blur'}
            // ],
            // stationName: [
            //     {required: true, message: '工位名称不能为空', trigger: 'blur'}
            // ],
            // abnormalNow: [
            //     {required: true, message: '异常现象不能为空', trigger: 'blur'}
            // ],
            // problemDescribe: [
            //     {required: true, message: '问题描述不能为空', trigger: 'blur'}
            // ],
            // proposal: [
            //     {required: true, message: '建议不能为空', trigger: 'blur'}
            // ]
        },
        abnormaSourceList: [
            {
                label: '维护规程',
                value: '1',
            }, {
                label: '维护计划',
                value: '2',
            }, {
                label: '巡检计划',
                value: '3',
            }
        ],
        equipmentNameList: [],
        abnormaData: {
            productionLineName: '',
            productionLine: '',
            workshopDept: '',
            planId: '',
            abnormalSources: '',
            equipmentId: '',
            stationNumber: '',
            abnormalNow: '',
            stationName: '',
            problemDescribe: '',
            proposal: '',
        },
        planApplyTypeList: [
            {
                label: '延期申请',
                value: '0'
            }, {
                label: '变更审批',
                value: '1'
            }
        ],
        columns3: [
            {
                title: '异常来源',
                key: 'ABNORMAL_SOURCES',
                align: 'center'
            }, {
                title: '状态',
                key: 'STATUS',
                align: 'center'
            }, {
                title: '车间',
                key: 'DNAME',
                align: 'center'
            }, {
                title: '生产线',
                key: 'TNAME',
                align: 'center'
            }, {
                title: '异常现象',
                key: 'ABNORMAL_NOW',
                align: 'center'
            }, {
                title: '问题描述',
                key: 'PROBLEM_DESCRIBE',
                align: 'center'
            }, {
                title: '建议',
                key: 'PROPOSAL',
                align: 'center'
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (function (h, params) {
                    return h('div', [h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            click: function click() {
                                vm.errorColorClick(params);
                            }
                        }
                    }, '异常关闭')]);
                })
                // render: (h, params) => {
                //     return h('div', [
                //         h('Button', {
                //             props: {type: 'primary', size: 'small'},
                //             style: {marginRight: '5px'},
                //             on: {
                //                 click: () => {
                //                     vm.errorColorClick(params)
                //                 }
                //             }
                //         }, '异常关闭')
                //     ]);
                // }
            }
        ],
        data3: [],
    },
    methods: {

        /**
         * 结束计划
         */
        endPlan: function () {
            var dataSource = getSelectedRowData("#jqGrid");
            if (!dataSource.id) {
                return;
            }
            if (!dataSource.carryNameDate) {
                return alert("该计划未执行!", "function")
            }
            confirm('确定关闭计划吗', function () {
                Ajax.request({
                    url: '../maintainplan/endPlan',
                    type: 'POST',
                    params: {'id': dataSource.id},
                    successCallback: function (index) {
                        alert(index.msg, function (index) {
                            vm.reload();
                        });
                    }
                })
            });

        },
        /**
         * 关闭异常
         * @param params
         */
        errorColorClick: function (params) {
            vm.data3 = [];
            $.ajax({
                url: '../maintainplan/upError',
                type: 'POST',
                data: {'id': params.row.ID},
                success: function (res) {
                    alert(res.msg, "function");
                    $.ajax({
                        url: '../maintainplan/allErrorRecording',
                        type: 'GET',
                        success: function (res) {
                            for (var dataList in res.allList) {
                                if (res.allList.hasOwnProperty(dataList)) {
                                    var data = res.allList[dataList];
                                    data.ABNORMAL_SOURCES = sysEnums.EEOR_SOURCE[data.ABNORMAL_SOURCES];
                                    data.STATUS = sysEnums.ABNORMAL_SOURCES[data.STATUS];
                                    vm.data3.push(data);
                                }
                            }
                        }
                    })
                    vm.query();
                }
            })
        },
        /**
         * 查看异常
         */
        queryError: function () {
            $.ajax({
                url: '../maintainplan/allErrorRecording',
                type: 'GET',
                success: function (res) {
                    for (var dataList in res.allList) {
                        if (res.allList.hasOwnProperty(dataList)) {
                            var data = res.allList[dataList];
                            data.ABNORMAL_SOURCES = sysEnums.EEOR_SOURCE[data.ABNORMAL_SOURCES];
                            data.STATUS = sysEnums.ABNORMAL_SOURCES[data.STATUS];
                            vm.data3.push(data);
                        }
                    }
                }
            })
            openWindow({
                title: '查看异常状态',
                content: jQuery("#queryError"),
            })
        },
        getTreeShow: function () {
            //加载分类树
            Ajax.request({
                url: '../sys/user/treeDeptByType/DT_CJKSCX',
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#categoryTree"), setting, r.list);
                    ztree.expandAll(true);
                }
            });
        },
        deptTreeShow: function () {
            openWindow({
                title: "选择生产线",
                area: ['300px', '450px'],
                content: jQuery("#categoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    vm.abnormaData.workshopDept = node[0].parentId;
                    if (node[0].deptType == 'DT_CJKSCX') {
                        // //选择上级菜单
                        vm.abnormaData.productionLine = node[0].deptId;
                        vm.abnormaData.productionLineName = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        /**
         * 异常提报
         */
        abnormalReport: function () {
            // vm.abnormaData = {};
            var dataSource = getSelectedRowData("#jqGrid");
            if (!dataSource.id) {
                return;
            }
            if (!dataSource.carryNameDate) {
                return alert("该计划未执行，不能提报异常", "function");
            }
            vm.equipmentNameList = [];
            $.ajax({
                url: '../maintainplan/allDevice',
                type: 'GET',
                success: function (res) {
                    for (var dataList in res.allList) {
                        if (res.allList.hasOwnProperty(dataList)) {
                            vm.equipmentNameList.push(res.allList[dataList]);
                        }
                    }
                }
            })
            vm.getTreeShow();
            openWindow({
                title: '异常提报',
                content: jQuery("#abnormalGrid"),
                area: ['50%', '95%'],
                btn: ['确定', '取消'],
                btn1: function (index) {
                    vm.abnormaData.planId = dataSource.id;
                    $.ajax({
                        url: '../maintainplan/inAbnormal',
                        type: 'POST',
                        data: vm.abnormaData,
                        success: function (value) {
                            alert(value.msg, "function");
                        }
                    })
                    layer.color(index);
                }
            })
        },
        /**
         * 通过
         * @param params
         */
        okClick: function (params) {
            $.ajax({
                url: '../maintainplan/upExecutive',
                type: 'POST',
                data: {'pid': params.row.maintainPlanId, 'id': params.row.id},
                success: function (index) {
                    alert(index.msg, "function");
                    vm.data2 = [];
                    $.ajax({
                        url: '../maintainplan/reviewPlanChange',
                        type: 'GET',
                        success: function (res) {
                            for (var dataList in res.allList) {
                                if (res.allList.hasOwnProperty(dataList)) {
                                    var data = res.allList[dataList]
                                    data.type = sysEnums.CHANGE_TYPE[data.type];
                                    vm.data2.push(data);
                                }
                            }
                        }
                    })
                    vm.query();
                }
            })
        },
        /**
         * 驳回
         * @param params
         */
        turnDownClick: function (params) {
            $.ajax({
                url: '../maintainplan/turnDownPlanChange',
                type: 'POST',
                data: {'id': params.row.id, 'maintainPlanId': params.row.maintainPlanId},
                success: function (index) {
                    alert(index.msg, "function");
                    vm.data2 = [];
                    $.ajax({
                        url: '../maintainplan/reviewPlanChange',
                        type: 'GET',
                        success: function (res) {
                            for (var dataList in res.allList) {
                                if (res.allList.hasOwnProperty(dataList)) {
                                    var data = res.allList[dataList]
                                    data.type = sysEnums.CHANGE_TYPE[data.type];
                                    vm.data2.push(data);
                                }

                            }
                        }
                    })
                    vm.query();
                }
            })
        },
        /**
         * 开始执行
         */
        startDate: function () {
            var dataList = getSelectedRowData("#jqGrid");
            if (!dataList.carryName) {
                return layer.alert("未分派执行人");
            }
            Ajax.request({
                url: '../maintainplan/upStartDate',
                type: 'POST',
                params: {"id": dataList.id},
                successCallback: function (index) {
                    vm.query();
                    alert(index.msg, "function");
                }
            })
        },
        /**
         * 延期审批
         */
        extensionApproval: function () {
            vm.data2 = [];
            $.ajax({
                url: '../maintainplan/reviewPlanChange',
                type: 'GET',
                success: function (res) {
                    for (var dataList in res.allList) {
                        if (res.allList.hasOwnProperty(dataList)) {
                            var data = res.allList[dataList]
                            data.type = sysEnums.CHANGE_TYPE[data.type];
                            vm.data2.push(data);
                        }

                    }
                }
            })
            openWindow({
                title: '计划审批',
                content: jQuery("#planChangeTable"),
                btn: ['关闭'],
                btn1: function (index) {
                    layer.close(index);
                }
            })
        },
        /**
         * 延期
         */
        extension: function () {
            vm.clear();
            var dataSource = getSelectedRowData("#jqGrid");//sysEnums.CHANGE_TYPE
            if (!dataSource.id) {
                return;
            }
            if (!dataSource.carryName) {
                return alert("该条计划无执行人，暂无法申请", "function");
            }
            // for (var dataType of sysEnums.CHANGE_TYPE) {
            //     vm.planApplyTypeList.push(dataType);
            // }
            $.ajax({
                url: '../maintainplan/countPlanChange',
                type: 'GET',
                data: {'id': dataSource.id},
                success: function (res) {
                    if (res.msg) {
                        alert(res.msg, "function");
                    } else {
                        vm.planApplyData.planName = dataSource.planName;
                        vm.planApplyData.maintainPlanId = dataSource.id;
                        openWindow({
                            title: '计划申请',
                            content: jQuery("#planApply"),
                            area: ['70%', '70%'],
                            btn: ['确定', '取消'],
                            btn1: function (index) {
                                $.ajax({
                                    url: '../maintainplan/insertPlanChange',
                                    type: 'POST',
                                    data: vm.planApplyData,
                                    success: function (index) {
                                        alert(index.msg, "function");
                                    }
                                })
                                layer.close(index);
                            }
                        })
                    }
                }
            })

        },
        /**
         * 分派
         */
        assignment: function () {
            vm.clear();
            var resourceData = getSelectedRowData("#jqGrid");
            vm.assignmentData.name = resourceData.planName;
            vm.assignmentData.review = resourceData.review;
            if (resourceData.carryName) {
                return alert("已分派", "function");
            }
            var personList = [];
            if (resourceData.length) {
                return;
            }
            if (!resourceData.review) {
                return alert("该条计划未评审", "function")
            }
            vm.cityList = [];
            vm.planPerson = [];
            vm.deptListPerson();
            openWindow({
                title: '分派任务',
                content: jQuery("#assignment"),
                area: ['40%', '70%'],
                btn: ['确定', '取消'],
                btn1: function (index) {
                    $.ajax({
                        url: '../maintainplan/dispatchPlan',
                        type: 'POST',
                        data: {
                            'id': resourceData.id,
                            'carryName': vm.assignmentData.carryName,
                            'minorPerson': JSON.stringify(vm.checkAllGroup)
                        },
                        success: function (res) {
                            layer.close(index);
                            vm.query();
                            vm.insertRecord(resourceData, vm.assignmentData.carryName);
                            alert(res.msg, "function");
                        }
                    })
                }
            })

        },
        /**
         * 分派人员
         */
        deptListPerson: function () {
            $.ajax({
                url: '../maintainplan/queryKeppDept',
                type: 'GET',
                success: function (value) {
                    if (value.deptList.length > 1) {
                        for (var dept in value.deptList) {
                            if (value.deptList.hasOwnProperty(dept)) {
                                var data = value.deptList[dept];
                                vm.cityList.push(data);
                                vm.planPerson.push(data);
                            }
                        }
                    }
                }
            })
        },
        /**
         * 主修人员
         * @param value
         */
        carryClick: function (value) {
            for (var size = 0; size < vm.planPerson.length; size++) {
                if (vm.planPerson[size].USER_NAME === value) {
                    vm.planPerson = [];
                    $.ajax({
                        url: '../maintainplan/queryKeppDept',
                        type: 'GET',
                        success: function (index) {
                            if (index.deptList.length > 1) {
                                for (var dept in index.deptList) {
                                    if (index.deptList.hasOwnProperty(dept)) {
                                        var data = index.deptList[dept];
                                        if (data.USER_NAME != value) {
                                            vm.planPerson.push(data);
                                        }
                                    }
                                }
                            }
                        }
                    })
                }
            }
        },
        /**
         * 清楚接口
         */
        clear: function () {
            vm.reviewValue = {
                name: '',
                review: '',
            };
            vm.assignmentData = {
                name: '',
                review: ''
            };
            vm.checkAllGroup = [];
            vm.planPerson = [];
            vm.planApplyData = {
                planName: '',
                reason: '',
                matter: '',
                type: '',
                maintainPlanId: '',
            };
        },
        /**
         * 评审并下发
         */
        reviewAndLower: function () {
            vm.clear();
            vm.title = '评判并下发';

            var resourceData = getSelectedRowData("#jqGrid");
            if (resourceData.length < 1) {
                return;
            }
            if (resourceData.review) {
                return alert("该条计划已评审", "function");
            }
            vm.reviewData = false;
            vm.reviewValue.name = resourceData.planName
            openWindow({
                title: '评审并下发',
                area: ['40%', '50%'],
                content: jQuery("#reviewData"),
                btn: ['下发', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url: '../maintainplan/upPlanReview',
                        type: 'POST',
                        params: {'reviewId': resourceData.id, 'reviewText': vm.reviewValue.review},
                        successCallback: function (res) {
                            if (!res.msg) {
                                alert('下发成功', 'function')
                                layer.close(index);
                                vm.query();
                            }
                        }
                    })
                }
            })
        },
        /**
         * 查询
         */
        query: function () {
            vm.reload();
        },
        /**
         * 返回
         * @param event
         */
        reload: function (event) {
            vm.addTab = true;
            vm.showList = true;
            vm.temporaryShow = false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        }
        , reload2: function (event) {
            vm.addTab = true;
            vm.showList = true;
            vm.temporaryShow = false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate1');
        },
        /**
         * 重置
         */
        reloadSearch: function () {

        },
        /**
         * 新增
         */
        add: function () {
            vm.showList = false;
            vm.addTab = false;
            vm.title = "新增";
        },
        /**
         * 编辑
         */
        update: function () {

        },
        /**
         * 删除
         */
        del: function () {
            var dataSource = getSelectedRows("#jqGrid");
            if (!dataSource) {
                return;
            }
            Ajax.request({
                url: '../maintainplan/delPlanPerson',
                type: 'POST',
                params: {'ids': JSON.stringify(dataSource)},
                successCallback: function (res) {
                    layer.alert(res.msg);
                    vm.reload();
                }
            })
        },
        /**
         * 重置
         * @param name
         */
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        /**
         * 提交
         */
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        /**
         * 插入维护记录选项关联
         */
        insertRecord: function (dateSource, carriedName) {
            var rId = '';
            $.ajax({
                url: '../maintainplan/allDevPlan',
                type: 'GET',
                data: {'id': dateSource.id},
                success: function (res) {
                    for (var dataList in res.allList) {
                        if (res.allList.hasOwnProperty(dataList)) {
                            var data = res.allList[dataList];
                            rId = data.ID;
                        }
                    }
                    $.ajax({
                        url: '../maintainplan/insertRecord',
                        type: 'POST',
                        data: {
                            'rId': rId,
                            'pId': dateSource.id,
                            'carriedDate': dateSource.carryDate,
                            'carriedName': carriedName
                        },
                        success: function (res) {
                        }
                    })
                }
            })

        },
        /**
         * 查看维护规程信息
         */
        reviewClick: function () {
            var dataSource = getSelectedRow("#jqGrid");
            vm.data1 = [];
            $.ajax({
                url: '../maintainplan/allDevPlan',
                type: 'GET',
                data: {'id': dataSource},
                success: function (res) {
                    for (var dataList in res.allList) {
                        if (res.allList.hasOwnProperty(dataList)) {
                            var data = res.allList[dataList];
                            data.FRE_RATE = sysEnums.FRERATE[data.FRE_RATE]
                            data.CHECK_STATUS = sysEnums.CHECK_FLAG[data.CHECK_STATUS]
                            data.OK_STATUS = sysEnums.CHECK_FLAG[data.OK_STATUS]
                            data.OK_DATE = vm.dateConversion(data.OK_DATE);
                            data.CHECK_DATE = vm.dateConversion(data.CHECK_DATE);
                            vm.data1.push(data);
                        }
                    }
                }
            })
            openWindow({
                title: '维护规程信息',
                content: jQuery("#maintenanceProcedure"),
                btn: ['关闭'],
                area: ['95%', '40%'],
                btn1: function (index) {
                    layer.close(index);
                }
            })
        },
        dateConversion: function (dataValue) {
            var date = new Date(dataValue);
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            var YMD = Y + M + D;
            return YMD;
        },
        assignmentClick: function () {
            vm.reviewClick();
        },
        //临时计划
        temporary: function () {
            vm.temporaryShow = true;
            vm.showList = false;
            vm.title = "新增";
        }, handleSubmit2: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saves()
            });
        }, handleReset: function (name) {
            handleResetForm(this, name);
            vm.handleResetRem('formValidateResource');
        }, saves: function () {
            var url = "../devmaintenplan/save";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devMaintenPlan),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        handleResetRem: function (name) {
            // handleResetForm(this, name);
        },
        saveOrUpdate: function (event) {
            var url = vm.maintainplan.id == null ? "../maintainplan/save" : "../maintainplan/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.maintainplan),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../maintainplan/infos/" + id,
                async: false,
                successCallback: function (r) {
                    vm.maintainplan2 = r.maintainPlan;
                }
            });
        },
        getPlan: function () {
            //获取审批通过规程
            openWindow({
                type: 2,
                title: '<i class="fa fa-list-ol"></i>添加规程',
                content: '../sys/addMaintainPlan.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDatas();
                    vm.maintainplan.ruleName = res.ruleName;
                    vm.maintainplan.ruleId = res.id;
                    layer.closeAll();
                }
            })
        },
        //pe人员审核
        apply: function () {
            var id = getSelectedRow("#jqGrid");
            if (!id) {
                return;
            }
            var data = getSelectedRowData("#jqGrid");
            if (data.approvalStatus != '未审批') {
                alert("当前记录已审批完成");
            } else {
                openWindow({
                    title: "确认审核",
                    area: ['400px', '240px'],
                    content: jQuery("#apply"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {

                        if (typeof (vm.maintainplan.approvalStatus) == 'undefined') {
                            alert("请选择审核状态");
                            return;
                        }
                        vm.maintainplan = {approvalStatus: vm.maintainplan.approvalStatus, id: id};
                        Ajax.request({
                            url: '../maintainplan/apply',
                            params: JSON.stringify(vm.maintainplan),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                    layer.closeAll();
                                });
                            }
                        });
                    }
                });
            }
        },
        //查看计划详情
        viewOption: function (plan_id) {
            //通过id  查询是否已经审核通过
            vm.getInfo(plan_id);
            if (vm.maintainplan2.approvalStatus != 1) {
                alert("当前记录未审核,无维护项目详情")
            } else {
                // openWindow({
                //     type: 2,
                //     title: '<i class="fa fa-pencil"></i> 维护项',
                //     content: '../device/devMainBaseCheck.html?id=' + plan_id
                // })
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-pencil"></i> 维护项',
                    content: '../device/devplandetailsView.html?plan_id=' + plan_id
                })


            }

        },
        //维护工单内容
        order: function (id) {
            //判断当前记录是否已经审批通过  ,未通过  无法查看
            vm.getInfo(id);
            if (vm.maintainplan2.approvalStatus != 1) {
                alert("当前记录未审核,无工单详情")
            } else {
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 查看详情',
                    content: '../device/DevworkOrderView.html?id=' + id
                })
            }
        }

    },

})
